package guitar

import (
	"context"
	"time"

	"go-weixin/app/guitar/api/internal/svc"
	"go-weixin/app/guitar/api/internal/types"
	"go-weixin/app/guitar/rpc/pb/proto/guitar"

	"github.com/zeromicro/go-zero/core/logx"
)

type GuitarGetImagesLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGuitarGetImagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GuitarGetImagesLogic {
	return &GuitarGetImagesLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GuitarGetImagesLogic) GuitarGetImages(req *types.GuitarGetImagesReq) (resp *types.GuitarGetImagesResp, err error) {
	ctx, cannFunc := context.WithTimeout(context.Background(), time.Second*5)
	defer cannFunc()
	res, err := l.svcCtx.GuitarRPC.GuitarGetImages(ctx, &guitar.GuitarGetImagesReq{
		Id:   req.Id,
		Page: req.Page,
	})
	if err != nil {
		return nil, err
	}
	var data types.GuitarJoinImage
	data.Id = res.Data.Id
	data.Title = res.Data.Title
	if len(res.Data.ImageList) > 0 {
		for _, m := range res.Data.ImageList {
			data.ImageList = append(data.ImageList, types.GuitarImage{
				Id:       m.Id,
				GuitarId: m.GuitarId,
				Url:      m.Url,
			})
		}
	}
	resp = new(types.GuitarGetImagesResp)
	resp.Data = data
	return
}
